﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Specialized;
using UnitTests.IISSimulator;
using FlexiCache;

namespace CLOComponentlibCSFixture.FlexiCache
{
    [TestClass()]
    public class CacheOnDemandProviderFixture
    {
        CacheOnDemandProvider _provider;

        [TestInitialize]
        public void BeforeTest()
        {
            _provider = new CacheOnDemandProvider();
            _provider.IsEnabled = true;
        }

        [TestCleanup]
        public void TearDown()
        {
            _provider.Dispose();
        }

        private void RunTest1()
        {
            string key = "12434";

            _provider.Add(key, "Hello MongoDB", DateTime.UtcNow.AddSeconds(100));
            var obj = _provider.Get(key);

            Assert.AreEqual("Hello MongoDB", obj);

            _provider.Remove(key);
        }

        [TestMethod]
        public void Test001_ShouldSaveNewObjectToCache()
        {
            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                sim.SimulateRequest();
                RunTest1();
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        [TestMethod]
        public void Test001_ShouldSaveNewObjectToCacheWithDisabled()
        {
            this._provider.IsEnabled = false;
            this.Test001_ShouldSaveNewObjectToCache();
        }

        [TestMethod]
        public void Test001_ShouldSaveNewObjectToCacheWithNoHttp()
        {
            RunTest1();
        }

        private void RunTest2()
        {
            string key = "45645464";

            _provider.RemoveAll();
            _provider.Add(key, "New Document", DateTime.UtcNow.AddSeconds(100));
            _provider.Set(key, "Updated Document", DateTime.UtcNow.AddSeconds(100));
            var obj = _provider.Get(key);

            Assert.AreEqual("Updated Document", obj);

            _provider.Remove(key);
        }

        [TestMethod]
        public void Test002_SetShouldUpdateExistingObjectFromCache()
        {
            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                sim.SimulateRequest();
                this.RunTest2();
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        [TestMethod]
        public void Test002_SetShouldUpdateExistingObjectFromCacheWithDisabled()
        {
            this._provider.IsEnabled = false;
            this.Test002_SetShouldUpdateExistingObjectFromCache();
        }

        [TestMethod]
        public void Test002_SetShouldUpdateExistingObjectFromCacheWithNoHttp()
        {
            this.RunTest2();
        }

        private void RunTest3()
        {
            string key = "08977";

            _provider.Add(key, "Document to be deleted", DateTime.UtcNow.AddSeconds(100));
            _provider.Remove(key);

            var obj = _provider.Get(key);

            Assert.IsNull(obj);
        }

        [TestMethod]
        public void Test003_ShouldRemoveValidObjectFromCache()
        {
            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                sim.SimulateRequest();
                this.RunTest3();
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        [TestMethod]
        public void Test003_ShouldRemoveValidObjectFromCacheWithDisabled()
        {
            this._provider.IsEnabled = false;
            this.Test003_ShouldRemoveValidObjectFromCache();
        }

        [TestMethod]
        public void Test003_ShouldRemoveValidObjectFromCacheWithNoHttp()
        {
            this.RunTest3();
        }
    }
}
